<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends BaseController {
    public function index(){
         if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
                $this->error("请先登录",U('Index/index'),1);
            }
        $this->display();
    }

    /*public function reg(){

        $user = M('user');

        $data = $user->create();

        //dump($data);
        if(!$data){

            $this->error($model->geterror());
            exit;
        };
        
        if($user->add() > 0){
                $d = $user->find();
                //$d['ip']=$_SERVER["REMOTE_ADDR"];
                //$d['ctime']=time();
                //$d['password']=md5($password);
                //$result = $m->add($d);
                //if($result>0){
                  //  if($this->SiteInfo['userstatus'] ==1){
                    //    $codeData['status'] =1;
                      //  $codeData['user']=$email;
                        //$codem->where("code = '{$code}'")->save($codeData);
                    //}
                    $users = M("userinfo");
                    $usersdata['id']=$d['id'];
                    $usersdata['headpic']='default.png';
                    $users->add($usersdata);
                    //echo $users->getLastsql;
            $this->success('注册成功',U('Index/index'));    
        }else{
            $this->error('注册失败');
        }
    }*/



    // 执行注册
    public function doreg(){
        $user = M('user');
        
        $data = $user->create();
        $data['password'] = md5($_POST["password"]);
        //dump($data);
        if (!$data) {
            $this->error($user->getError());
        }
        if ($result = $user->add($data)) {
                    //dump($result);
                    $info = D("userinfo");
                    $usersdata = array();
                    $usersdata['uid']=$result;
                    $usersdata['headpic']='default.png';
                    $pro = $info->add($usersdata);
            $this->success('注册成功！', U('index'));
        }else{
            $this->error('信息表生成失败', U('index'));
        }

    }

    // 校验用户名是否存在
    public function checkuName(){
        $user = I('post.user');
        $model = M('user');
        $options = array();
        $options['username'] = $user;
        $data = $model->where($options)->getField('id');
        
        $this->ajaxReturn($data);
    }

   public function checkEmail(){
        $email = I('post.email');
        $model = M('user');
        $options = array();
        $options['email'] = $email;
        $data = $model->where($options)->getField('id');
        $this->ajaxReturn($data);
    }
    

   

   public function checkPhone(){  
        $phone = I('post.phone');
        $model = M('user');
        $options = array();
        $options['phone'] = $phone;
        $data = $model->where($options)->getField('id');
        $this->ajaxReturn($data);
    }

    
    //生成验证码
        public function yanzheng(){
        $config = array(
        'fontSize' => 40, // 验证码字体大小
        'length' => 4, // 验证码位数

        'useCurve'  =>  false,            // 是否画混淆曲线
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
        }
    // 验证码校验
    public function code(){
        $code = I('post.code');
        $verify = new \Think\Verify();
        $data = $verify->check($code, $id = '');
        $this->ajaxReturn($data);
    }
    // 存放输入的验证码
    public function rcode(){
        $rcode = I('post.rcode');
        $_SESSION['code'] = $rcode; 
    }

////////////////登陆用////////////
    //生成验证码
        public function yanzhengma(){
        $config = array(
        'fontSize' => 40, // 验证码字体大小
        'length' => 4, // 验证码位数
        'useCurve'  =>  false,            // 是否画混淆曲线
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
        }
    // 验证码校验
    public function codema(){
        $codema = I('post.codema');
        $verify = new \Think\Verify();
        $data = $verify->check($codema, $id = '');
        $this->ajaxReturn($data);
    }
    // 存放输入的验证码
    public function rcodema(){
        $rcodema = I('post.rcodema');
        $_SESSION['codema'] = $rcodema; 
    }


        
	


    public function detail(){
            if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
                $this->error("请先登录",U('Index/index'));
            }
            $id = $_SESSION['fix_user']['id'];
            $m = M("userinfo");
            $arr = $m->where("uid = {$id}")->find();
            //dump($arr);
            $this->assign("detail",$arr);
            $this->display();
        }

    public function saveDetail(){
            $id = $_SESSION['fix_user']['id'];
            $m=M("userinfo");
            $result=$m->where("uid = {$id}")->save($m->create());
            //dump($result);
            if($result>0){
                $this->success("更新成功！");
            }else{
                $this->error("更新失败！");
            }
        }
	//执行登录
   
  /**  public function login(){
        $username = I('post.username');
        $password = I('post.password');
    
        $data = M('user')->where(array('username'=>$username))->find();
        //dump($data);
    
        if (is_null($data)) {
            $this->error('用户名错误!');
            exit;
        }

        //密码判断
        if ($data['password'] !== $password) {
            $this->error('密码错误');
            exit;
        }

        //登录成功
        $_SESSION['fix_user'] = $data;

        //成功跳转
        $this->redirect('index/index', '', 3, 'Login ....');

    }
    **/
	public function gonggao(){
            if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
                $this->error("请先登录",U('Index/index'));
            }
            $m = M("notice");
            $arr = $m->select();
            $this->assign("arr",$arr);
            $this->display();
        }

    public function login(){
        $username = $_POST['username'];
        $password = md5($_POST['password']);
        $m=M("user");
        $result = $m->where("username = '{$username}' AND password = '{$password}'")->find();
        if($result>0){
            $_SESSION['fix_user'] = $result;
            $_SESSION['fix_user']['logintime']=$result['logintime'];
            $m->where("id = {$result['id']}")->setField('logintime',time());
            //$_SESSION['muser']=$result['truename'];
            //$_SESSION['mid']=$result['id'];
            //$_SESSION['logintime']=$result['logintime'];
            //$m->w here("id = {$result['id']}")->setField('logintime',time());
            //$_SESSION['mlastip']=$result['ip'];
            //$m->where("id = {$result['id']}")->setField('ip',$_SERVER["REMOTE_ADDR"]);
            $this->success("登陆成功啊哈哈哈!", U('index/index'));
        }
        if($_POST['password'] !== $result['password'] || $_POST['username'] !== $result['username']){
            $this->error("用户名或密码错误!");
        }

    
    }

    public function changeHeadpic(){
            if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
                $this->error("请先登录",U('Index/index'));
            }
            $this->display();
        }
    public function changepwd(){
            if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
                $this->error("请先登录",U('Index/index'));
            }
            $this->display();
        }

    public function changePass(){
            if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
                $this->error("请先登录",U('Index/index'));
            }
            $one = $_POST['passone'];
            $two = $_POST['passtwo'];
            if($one != $two || $one == '' || $two == ''){
                $this->error("对不起，两次密码不一致");
            }else{
                $m=M("user");
                $id = $_SESSION['fix_user']['id'];

                $result = $m->where("id={$id}")->save(array('password'=>$one));
                //echo $m->getLastsql();
               // dump($result);
                if($result>0){
                    
                    $this->success("修改成功!",U('index/index'));
                    unset($_SESSION['fix_user']);
                }else{
                    $this->error("修改失败！");
                }
            }
        }

    public function userInfo(){
            if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
                $this->error("请先登录",U('Index/index'));
            }
            $m=M("user");
            $mm =M("userinfo");
            $userid = $_SESSION['fix_user']['id'];
            $userInfo = $mm->where("id={$userid}")->find();
            
            $this->assign("userInfo",$userInfo);
                $this->display();
            }
    //执行修改
     public function changeInfo(){
            $m=M("user");

            //$mm = M("_SESSION");

            $userid = $_SESSION['fix_user']['id'];
            $nickname = $_POST['nickname'];
            $age = $_POST['age'];
            $data = $m->save(array('id'=>$userid,'nickname'=>$nickname,'age'=>$age));
            $result = $m->where("id={$userid}")->find();
            $_SESSION['fix_user']['age'] = $result['age'];
            $_SESSION['fix_user']['nickname'] = $result['nickname'];
            //dump($data);
            //dump($result);
            echo $m->getLastsql();
            //数据创建
            
            if($data > 0){
               $this->success('修改成功',U('index'));
            }else{
               $this->error('修改失败');
            }


        }

    public function logout(){
        unset($_SESSION['fix_user']);
        $this->success("退出成功！");

    }

    public function myArticle(){
        if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
            $this->error("请先登录",U('Index/index'));
        }
        $uid = $_SESSION['fix_user']['id'];
        $m = M("text");
        $count      = $m->where("bid = {$uid} AND status = 1")->count();//
        $Page       = new \Think\Page($count,10);//
        //dump($Page);
        $show       = $Page->show();//
        //dump($show);
        $articleList = $m->where("bid = {$uid} AND status = 1")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("articleList",$articleList);
        $this->display();
    }

    public function delArticle(){
            if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
                $this->error("请先登录",U('Index/index'));
            }
            $uid = $_SESSION['fix_user']['id'];
            $id = $_GET['id'];
            $m = M("text");
            $articleInfo= $m->where("id = {$id}")->find();
            if($articleInfo['bid'] != $uid){
                $this->error("请先登录",U('Index/index'));
            }else{
                $result = $m->where("id = {$id}")->setField('status',0);
                if($result>0){
                    $this->success("删除成功！");
                }else{
                    $this->error("删除失败！");
                }
            }
        }

    public function addArticle(){
        if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
            $this->error("请先登录",U('Index/index'));
        }else{
            $m=M("pathclass");
            $arr = $m->where("fid != 0")->select();
            $this->assign("arr",$arr);
            $this->display();
        }

    }

    public function editArticle(){
        $id = $_GET['id'];
        if(empty($id)){
            $this->error("请先登录");
        }else{
            $m = M("text");
            $id = $_GET['id'];
            $arr = $m->where("id = {$id}")->find();
            $this->assign("edit",$arr);

            $m=M("pathclass");
            $fenlei = $m->where("fid != 0")->select();
            $path = $m->where("id = $arr[tid]")->find();
            $this->assign("fenlei",$fenlei);
            $this->assign("path",$path);
            $this->display();
        }

    }

    public function doEditArticle(){
        if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
            $this->error("请先登录",U('Index/index'));
        }else{
            $m=M("text");
            $u = M('user');
            $data = $m->create();
            $data['id'] = $_POST['id'];
            $data['bid']= $_SESSION['fix_user']['id']; //获取文章作者的id
            $data['edittime']=time();
            $data['content'] = $_POST['content'];
            $data['tid'] = $_POST['tid'];
            $data['mp3'] = $_POST['mp3'];
            $data['picname'] = $_POST['picname'];
            //dump($data);
            //判断文章类型
            $mm = M("pathclass");
            $fidInfo = $mm->where("id={$data['tid']}")->find();
            //dump($fidInfo);
            if($fidInfo['type']==2 && $_FILES['mp3']['name'] != ''){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     20971520 ;// 设置附件上传大小
                $upload->exts      =     array('mp3', 'wam', 'wma', 'aac','mod','cd');// 设置附件上传类型
                $upload->rootPath  =      './Public/Uploads/'; // 设置附件上传目录    // 上传文件
                $info   =   $upload->uploadOne($_FILES['mp3']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $data['mp3']=$info['savepath'].$info['savename'];
                }
            }elseif($fidInfo['type']==2 && $_FILES['mp3']['name'] == ''){
                unset($data['mp3']);
            }
            if($_FILES['picname']['name'] != ''){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =      './Public/Uploads/'; // 设置附件上传目录    // 上传文件
                $info   =   $upload->uploadOne($_FILES['picname']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功
                    $data['picname']=$info['savepath'].$info['savename'];
                }
            }else{
                unset($data['picname']);
            }
                    $result=$m->where("id = {$data['id']}")->save($data);
                    //$result = $m->save($data);
                    if($result>0){
                        /*if(!$this->email_set['send_article_set']){
                            $m = M('email_type');
                            $info= $m->find(1);
                            $url = "http://".$_SERVER['SERVER_NAME'].__MODULE__."/Article/index/id/".$result;
                            $info['send_article_content'] = $info['send_article_content']."<br/>发表文章标题：<a href = '".$url."' target = '_blank'>{$_POST['title']}</a>";
                            $this->MySmtp($this->admin_email,$info['send_article_title'],$info['send_article_content']);
                        }*/
                        
                        $this->success("修改成功！");
                    }else{
                        $this->error("修改失败！");
                    }
                    //echo $m->getLastsql();
                    //dump($data);
                }
            }
        

    

    public function doAddArticle(){
        if($_SESSION['fix_user']==null || $_SESSION['fix_user']== ''){
            $this->error("请先登录",U('Index/index'));
        }else{
            $m=M("text");
            $u = M('user');
            $data = $m->create();
            $data['bid']= $_SESSION['fix_user']['id']; //获取文章作者的id
            $data['createtime']=time(); //创建时间
            $data['edittime']=time();
            $data['view']=1;
            $data['content'] = $_POST['content'];
            $data['tid'] = $_POST['tid'];
            //判断文章类型
            $mm = M("pathclass");
            $fidInfo = $mm->where("id={$data['tid']}")->find();
            //dump($fidInfo);
            if($fidInfo['type']==2){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     20971520 ;// 设置附件上传大小
                $upload->exts      =     array('mp3', 'wam', 'wma', 'aac','mod','cd');// 设置附件上传类型
                $upload->rootPath  =      './Public/Uploads/'; // 设置附件上传目录    // 上传文件
                $info   =   $upload->uploadOne($_FILES['mp3']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $data['mp3']=$info['savepath'].$info['savename'];
                }
            }elseif ($fidInfo['type']==4) {
                //原来是两张图片，现在取消了
            }
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =      './Public/Uploads/'; // 设置附件上传目录    // 上传文件
                $info   =   $upload->uploadOne($_FILES['picname']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功
                    $data['picname']=$info['savepath'].$info['savename'];
                    $result = $m->add($data);
                    if($result>0){
                        /*if(!$this->email_set['send_article_set']){
                            $m = M('email_type');
                            $info= $m->find(1);
                            $url = "http://".$_SERVER['SERVER_NAME'].__MODULE__."/Article/index/id/".$result;
                            $info['send_article_content'] = $info['send_article_content']."<br/>发表文章标题：<a href = '".$url."' target = '_blank'>{$_POST['title']}</a>";
                            $this->MySmtp($this->admin_email,$info['send_article_title'],$info['send_article_content']);
                        }*/
                        //dump($data);
                        //echo $m->getLastsql();
                        $this->success("发布成功！");
                    }else{
                        $this->error("发布失败！");
                    }
                }
            }
        }

        public function liuyan(){
            if($_SESSION['fix_user'] == null || $_SESSION['fix_user'] == ''){
                $this->error("请先登录",U('Index/index'));
            }
            $id = $_SESSION['fix_user']['id'];
            $m = M('grliuyan');
            $arr = $m->where("uid = {$id}")->select();
            //dump($arr);
            $this->assign('asb',$arr);
            $this->display();
        }

        
}